<?php
/**
 * User Class
 */
class User extends Controller {
	/**
	 * Constructor, load Semester_model, Cabang_model
	 */
	function User()
	{
		parent::Controller();
		$this->load->model('User_model', '', TRUE);
		$this->load->model('Cabang_model', '', TRUE);
		$this->load->model('Usergroup_model', '', TRUE);
	}
	
	/**
	 * Iid_userialisasi variabel untuk $title(untuk id element <body>), dan 
	 * $limit untuk membatasi penampilan data di tabel
	 */
	var $limit = 10;
	var $title = 'user';
	
	/**
	 * Memeriksa user state, jika dalam keadaan login akan menjalankan fungsi get_all()
	 * jika tidak akan meredirect ke halaman login
	 */
	function index()
	{
		if ($this->session->userdata('login') == TRUE )
		{
			if ($this->session->userdata('id_usergroup') == 6 || $this->session->userdata('id_usergroup') == 1 )
			{
				$this->get_all();
			}
			else
			{
				redirect('home');
			}
		}
		else
		{
			redirect('login');
		}
	}
	
	/**
	*rubah pass personal
	*/
	
	function change_pass($id_user)
	{
		$data['title'] = $this->title;
		$data['h2_title'] = 'User';
		$data['main'] = 'user/user_passform';
		$data['form_action']	= site_url('user/change_pass_process');
		$data['link'] 			= array('link_back' => anchor('home/','kembali', array('class' => 'back'))
										);
		

		if ($this->session->userdata('id_user') == $id_user )
			{
				$this->load->view('base', $data);
			}
			else
			{
				redirect('home');
			}				
	}
	
	function change_pass_process()
	{	
		$data['title'] = $this->title;
		$data['h2_title'] = 'User';
		$data['main'] = 'user/user_passform';
		$data['form_action']	= site_url('user/change_pass_process');
		$data['link'] 			= array('link_back' => anchor('home/','kembali', array('class' => 'back'))
										);
		
		
		// Set validation rules
		$this->form_validation->set_rules('password', 'Password', 'required|max_length[50]');
		
		// jika proses validasi sukses, maka lanjut mengupdate data
		if ($this->form_validation->run() == TRUE)
		{
			// save data
			$data_user = array('password'		=> $this->encrypt->SHA1($this->input->post('password')));
			$this->User_model->update($this->session->userdata('id_user'), $data_user );
			
			// set pesan
			$this->session->set_flashdata('message', 'Password berhasil diupdate!');
					
			redirect('home');
		}
		else
		{
			$this->load->view('base', $data);
		}
	}
	
	/**
	 * Mendapatkan semua data user di database dan menampilkannya di tabel
	 */
	function get_all($offset = 0)
	{
		$data['title'] = $this->title;
		$data['h2_title'] = 'User';
		$data['main'] = 'user/user';
		
		// Offset
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		
		// Load data
		if ($this->session->userdata('id_usergroup') == 6)
			{
				$user = $this->User_model->get_all($this->limit, $offset);
				$num_rows = $this->User_model->count_all();
			}
			else
			{
				$user = $this->User_model->get_all_by_cabang($this->limit, $offset, $this->session->userdata('id_cabang'));
				$num_rows = $this->User_model->count_all_by_cabang($this->session->userdata('id_cabang'));
			}
		
		if ($num_rows > 0)
		{
			// Generate pagination			
			$config['base_url'] = site_url('user/get_all');
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->limit;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			
			// Table
			/*Set table template for alternating row 'zebra'*/
			$tmpl = array( 'table_open'    => '<table border="0" cellpadding="0" cellspacing="0">',
						  'row_alt_start'  => '<tr class="zebra">',
							'row_alt_end'    => '</tr>'
						  );
			$this->table->set_template($tmpl);

			/*Set table heading */
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No', 'Nama User', 'Usergroup', 'Cabang', 'Actions');
			$i = 0 + $offset;
			
			if($this->session->userdata('id_usergroup') == 6)
			{
				foreach ($user as $row)
				{
					$this->table->add_row(++$i, $row->user_name, $row->usergroup, $row->cabang,
											anchor('user/update/'.$row->id_user,'update',array('class' => 'update')).' '.
											anchor('user/delete/'.$row->id_user,'hapus',array('class'=> 'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')"))
											);
				}
				$data['table'] = $this->table->generate();
			} else
			{
				foreach ($user as $row)
				{
					$this->table->add_row(++$i, $row->user_name, $row->usergroup, $row->cabang,
											anchor('user/update/'.$row->id_user,'update',array('class' => 'update')).' '.
											anchor('user/delete/'.$row->id_user,'hapus',array('class'=> 'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')"))
											);
				}
				$data['table'] = $this->table->generate();
			}
			
		}
		else
		{
			$data['message'] = 'Tidak ditemukan satupun data user!';
		}		
		
		$data['link'] = array('link_add' => anchor('user/add/','tambah data', array('class' => 'add'))
								);
		
		// Load view
		$this->load->view('base', $data);
	}
	
	/**
	 * Menghapus data user dengan ID User tertentu
	 */
	function delete($id_user)
	{
		$this->User_model->delete($id_user);
		$this->session->set_flashdata('message', '1 data user berhasil dihapus');
		
		redirect('user');
	}
	
	/**
	 * Menampilkan form tambah user
	 */
	function add()
	{			
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'User > Tambah Data';
		$data['main'] 			= 'user/user_form';
		$data['form_action']	= site_url('user/add_process');
		$data['link'] 			= array('link_back' => anchor('user','kembali', array('class' => 'back'))
										);
		// data usergroup untuk dropdown menu
		$usergroup = $this->Usergroup_model->get_usergroup()->result();
		foreach($usergroup as $row)
		{
			$data['options_usergroup'][$row->id_usergroup] = $row->usergroup;
		}
		//jika bukan superadmin remove opsi buat akun superadmin
		if ($this->session->userdata('id_usergroup') == 1 )
		{
			unset ($data['options_usergroup'][6]);
		}
		
		// data cabang untuk dropdown menu
		if ($this->session->userdata('id_usergroup') == 1 )
			{
				$cabang = $this->Cabang_model->get_cabang2($this->session->userdata('id_cabang'))->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
			else //superadmin
			{
				$cabang = $this->Cabang_model->get_cabang()->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
		
		
		$this->load->view('base', $data);
	}
	/**
	 * Proses tambah data user
	 */
	function add_process()
	{		
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'User > Tambah Data';
		$data['main'] 			= 'user/user_form';
		$data['form_action']	= site_url('user/add_process');
		$data['link'] 			= array('link_back' => anchor('home/','kembali', array('class' => 'back'))
										);
		
		// data usergroup untuk dropdown menu
		$usergroup = $this->Usergroup_model->get_usergroup()->result();
		foreach($usergroup as $row)
		{
			$data['options_usergroup'][$row->id_usergroup] = $row->usergroup;
		}
		//jika bukan superadmin remove opsi buat akun superadmin
		if ($this->session->userdata('id_usergroup') == 1 )
		{
			unset ($data['options_usergroup'][6]);
		}
		
		// data cabang untuk dropdown menu
		if ($this->session->userdata('id_usergroup') == 1 )
			{
				$cabang = $this->Cabang_model->get_cabang2($this->session->userdata('id_cabang'))->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
			else //superadmin
			{
				$cabang = $this->Cabang_model->get_cabang()->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
		
		// Set validation rules
		$this->form_validation->set_rules('user_name', 'User Name', 'required|max_length[50]');
		$this->form_validation->set_rules('password', 'Password', 'required|max_length[50]');
		$this->form_validation->set_rules('id_cabang', 'Cabang', 'required');
		$this->form_validation->set_rules('id_usergroup', 'Usergroup', 'required');
		
		if ($this->form_validation->run() == TRUE)
		{
			// save data
			$user = array('user_name'		=> $this->input->post('user_name'),
							'password'		=> $this->encrypt->SHA1($this->input->post('password')),
							'id_cabang'		=> $this->input->post('id_cabang'),
							'id_usergroup'		=> $this->input->post('id_usergroup')
						);
			$this->User_model->add($user);
			
			$this->session->set_flashdata('message', 'Satu data user berhasil disimpan!');
			redirect('user/add');
		}
		else
		{	
			$data['default']['id_cabang'] = $this->input->post('id_cabang');
			$data['default']['id_usergroup'] = $this->input->post('id_usergroup');
			$this->load->view('base', $data);
		}		
	}
	
	/**
	 * Menampilkan form update data user
	 */
	function update($id_user)
	{	
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'User > Update';
		$data['main'] 			= 'user/user_form';
		$data['form_action']	= site_url('user/update_process');
		$data['link'] 			= array('link_back' => anchor('user','kembali', array('class' => 'back'))
										);
		
		// data usergroup untuk dropdown menu
		$usergroup = $this->Usergroup_model->get_usergroup()->result();
		foreach($usergroup as $row)
		{
			$data['options_usergroup'][$row->id_usergroup] = $row->usergroup;
		}
		//jika bukan superadmin remove opsi buat akun superadmin
		if ($this->session->userdata('id_usergroup') == 1 )
		{
			unset ($data['options_usergroup'][6]);
		}
		
		// data cabang untuk dropdown menu
		if ($this->session->userdata('id_usergroup') == 1 )
			{
				$cabang = $this->Cabang_model->get_cabang2($this->session->userdata('id_cabang'))->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
			else //superadmin
			{
				$cabang = $this->Cabang_model->get_cabang()->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
		
		// cari data dari database
		$user = $this->User_model->get_user_by_id($id_user);
		
		// buat session untuk menyimpan data primary key (id_user)
		$this->session->set_userdata('id_user', $user->id_user);
		
		// Data untuk mengisi field2 form	
		$data['default']['user_name'] 		= $user->user_name;	
		$data['default']['password'] 		= $user->password;			
		$data['default']['id_cabang']		= $user->id_cabang;		
		$data['default']['id_usergroup']	= $user->id_usergroup;
				
		$this->load->view('base', $data);
	}
	
	/**
	 * Proses update data user
	 */
	function update_process()
	{		
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'User > Update';
		$data['main'] 			= 'user/user_form';
		$data['form_action']	= site_url('user/update_process');
		$data['link'] 			= array('link_back' => anchor('user','kembali', array('class' => 'back'))
										);
		
		// data usergroup untuk dropdown menu
		$usergroup = $this->Usergroup_model->get_usergroup()->result();
		foreach($usergroup as $row)
		{
			$data['options_usergroup'][$row->id_usergroup] = $row->usergroup;
		}
		//jika bukan superadmin remove opsi buat akun superadmin
		if ($this->session->userdata('id_usergroup') == 1 )
		{
			unset ($data['options_usergroup'][6]);
		}
		
		// data cabang untuk dropdown menu
		if ($this->session->userdata('id_usergroup') == 1 )
			{
				$cabang = $this->Cabang_model->get_cabang2($this->session->userdata('id_cabang'))->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
			else //superadmin
			{
				$cabang = $this->Cabang_model->get_cabang()->result();
				foreach($cabang as $row)
				{
					$data['options_cabang'][$row->id_cabang] = $row->cabang;
				}
			}
			
		// Set validation rules
		$this->form_validation->set_rules('user_name', 'User Name', 'required|max_length[50]');
		$this->form_validation->set_rules('password', 'Password', 'required|max_length[50]');
		$this->form_validation->set_rules('id_cabang', 'Cabang', 'required');
		$this->form_validation->set_rules('id_usergroup', 'Usergroup', 'required');
		
		// jika proses validasi sukses, maka lanjut mengupdate data
		if ($this->form_validation->run() == TRUE)
		{
			// save data
			$data_user = array('user_name'		=> $this->input->post('user_name'),
							'password'		=> $this->encrypt->SHA1($this->input->post('password')),
							'id_cabang'	=> $this->input->post('id_cabang'),
							'id_usergroup'	=> $this->input->post('id_usergroup')
						);
			$this->User_model->update($this->session->userdata('id_user'), $data_user );
			
			// set pesan
			$this->session->set_flashdata('message', 'Satu data user berhasil diupdate!');
			
			redirect('user');
		}
		else
		{
			$data['default']['id_cabang'] = $this->input->post('id_cabang');
			$data['default']['id_usergroup'] = $this->input->post('id_usergroup');
			$this->load->view('base', $data);
		}
	}
	
	/**
	 * Validasi untuk id_user, agar tidak ada user dengan ID User sama
	 */
	function valid_id_user($id_user)
	{
		if ($this->User_model->valid_id_user($id_user) == TRUE)
		{
			$this->form_validation->set_message('valid_id_user', "user dengan ID User $id_user sudah terdaftar");
			return FALSE;
		}
		else
		{			
			return TRUE;
		}
	}
	
	// cek apakah valid untuk update?
	function valid_id_user2()
	{
		// cek agar tidak ada id_user ganda, khusus untuk proses update
		$current_id_user 	= $this->session->userdata('id_user');
		$new_id_user		= $this->input->post('id_user');
				
		if ($new_id_user === $current_id_user)
		{
			return TRUE;
		}
		else
		{
			if($this->User_model->valid_id_user($new_id_user) === TRUE) // cek database untuk entry yang sama memakai valid_entry()
			{
				$this->form_validation->set_message('valid_id_user2', "User dengan id_user $new_id_user sudah terdaftar");
				return FALSE;
			}
			else
			{
				return TRUE;
			}
		}
	}

}
// END User Class

/* End of file user.php */
/* Location: ./system/application/controllers/user.php */